package org.sbbs.app.security.webapp.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.dispatcher.Dispatcher;
import org.sbbs.app.security.model.UrlSecurityResource;
import org.sbbs.app.security.service.UrlSecurityResourceManager;
import org.sbbs.base.webapp.action.BaseJqgridEditAction;

import com.opensymphony.xwork2.config.Configuration;
import com.opensymphony.xwork2.config.ConfigurationManager;
import com.opensymphony.xwork2.config.RuntimeConfiguration;
import com.opensymphony.xwork2.config.entities.ActionConfig;

public class UrlSecurityResourceEditAction extends BaseJqgridEditAction {

	private UrlSecurityResource model;
	private UrlSecurityResourceManager urlSecurityResourceManager;

	public UrlSecurityResource getModel() {
		return model;
	}

	public void setModel(UrlSecurityResource model) {
		this.model = model;
	}

	public UrlSecurityResourceManager getUrlSecurityResourceManager() {
		return urlSecurityResourceManager;
	}

	public void setUrlSecurityResourceManager(
			UrlSecurityResourceManager urlSecurityResourceManager) {
		this.urlSecurityResourceManager = urlSecurityResourceManager;
	}

	public String edit() {
		if (getModelId() != null) {
			this.setModel(this.urlSecurityResourceManager.get(Long
					.parseLong(this.getModelId())));
			this.setEditType(EDIT_TYPE_EDIT);
		} else {
			this.setModel(new UrlSecurityResource());
			this.setEditType(EDIT_TYPE_NEW);
		}
		return this.SUCCESS;

	}

	public String save() {
		try {
			HttpServletRequest req = this.getRequest();
			boolean isNew = (this.getEditType() == 1);
			this.getUrlSecurityResourceManager().save(
					(UrlSecurityResource) model);
			this.setMessage(getText((isNew)
					? "urlSecurityResource.added"
					: "urlSecurityResource.updated",
					"no msg key found,save successed."));
		} catch (Exception e) {
			this
					.setMessage(getText("error.saved", new String[]{e
							.getMessage()}));
			this.setReturnCode(RETURN_STATUS_ERROR);
		}
		return this.SUCCESS;
	}

	public String delete() {
		try {

			StringTokenizer ids = new StringTokenizer(this.getDelIds(), ",");
			while (ids.hasMoreTokens()) {
				Long removeId = Long.parseLong(ids.nextToken());
				this.getUrlSecurityResourceManager().remove(removeId);
			}
			this.setMessage(getText("urlSecurityResource.deleted",
					"no msg key found,delete successed."));
		} catch (Exception e) {
			this.setMessage(getText("error.deleted", new String[]{e
					.getMessage()}));
			this.setReturnCode(RETURN_STATUS_ERROR);
		}
		return this.SUCCESS;
	}

	public String initUrlResourceRepo() {
		try {
			List resList = this.urlSecurityResourceManager.getAll();

			Map rsMap = new HashMap();

			for (Iterator iterator = resList.iterator(); iterator.hasNext();) {
				UrlSecurityResource res = (UrlSecurityResource) iterator.next();
				// res.setId(null);
				rsMap.put(res.getUrl(), res);

			}

			Dispatcher dispatcher = Dispatcher.getInstance();

			ConfigurationManager cm = dispatcher.getConfigurationManager();

			Configuration cf = cm.getConfiguration();

			RuntimeConfiguration rc = cf.getRuntimeConfiguration();

			Map<String, Map<String, ActionConfig>> d = rc.getActionConfigs();
			Set nc = d.keySet();
			for (String key1 : d.keySet()) {
				if (!key1.equalsIgnoreCase("")
						&& !key1.equalsIgnoreCase("/config-browser")) {
					System.out.println(key1);
					if (key1.equalsIgnoreCase("/")) {
						key1 = key1;
					} else {
						key1 = key1 + "/";
					}
					Map<String, ActionConfig> a = d.get(key1);
					for (String key2 : a.keySet()) {
						System.out.println(key2);

						UrlSecurityResource res = new UrlSecurityResource();
						res.setUrl(key1 + key2);
						/*
						 * ResourceBundle rb =
						 * this.getTexts("org/sbbs");
						 * this.gett String resName =
						 * this
						 * .getTexts("").getString(key2
						 * +".title");
						 */

						res.setName(this.getText(key2 + ".title", key2));

						if (!rsMap.containsKey(key1 + key2)) {
							rsMap.put(key1 + key2, res);
						}
						// resList.add(res);
						ActionConfig ac = a.get(key2);
					}
				}
			}

			this.urlSecurityResourceManager.initUrlResourceRepo(new ArrayList(
					rsMap.values()));

			this.setMessage(getText("urlSecurityResource.repoInited",
					"no msg key found,url resources init successed."));
		} catch (Exception e) {
			this.setMessage(getText("error.common",
					new String[]{e.getMessage()}));
			this.setReturnCode(RETURN_STATUS_ERROR);
		}

		return this.SUCCESS;
	}
}
